import {
	objDeepCopy,
	getDateString
} from "@/assets/js/common.js";
import commonObject from "@/assets/js/xm.js";
let Controller = {
	created() {
		this.init();
	},
	mounted() {

	},
	methods: {
		init() {
			this.getAllocationList(this);
			// this.getinoutStatus(this);

		},
		//获取列表接口
		getAllocationList(that, params, page) {
			params = params || {
				countType: 3
			}
			page = page || that.Model.page;
			var apiTest = this.apiTest;
			this.$ajax({
				method: 'post',
				url:apiTest+'/supplier/whInventoryMain/list',
				// url: '/api/whInventoryMain/list',
				data: {
					condition: params,
					pageIndex: page.pageIndex,
					pageSize: page.pageSize,
				}
			}).then((res) => {
				that.Model.tableData = [];
				var data = res.data;
				if (data.status == 200) {
					var obj = {};
					var result = data.result.result;
					var pagination = commonObject.paginationFunction(data.result);
					that.Model.paginationObject = objDeepCopy(pagination);
					if (result) {
						that.Model.tableData = objDeepCopy(result);
					}
				} else {
					that.tips(that, 'warning', data.msg);
				}

			});
		},

		//跳转到详情页
		toDetial(row, type) {
			this.$router.push({
				path: 'dipInventoryNew',
				query: {
					id: row.id,
					type: type
				}
			});
		},
		//点击 新建抽盘任务
		newDip(row) {
			this.Model.importFileDialog = true;
			/* this.$confirm('系统会根据商品类型自动筛选抽盘的商品，是否确定创建抽盘任务？', '提示', {
			  confirmButtonText: '确定',
			  cancelButtonText: '取消',
			}).then(() => {
				this.newDipAjax(this)
			}).catch(() => {
			    
			}); */
		},
		//新建抽盘任务 请求
		newDipAjax(that) {
			var apiTest = this.apiTest;
			this.$ajax({
				method: 'post',
				url:apiTest+'/supplier/whInventoryMain/add',
				// url: '/api/whInventoryMain/add',
				data: {
					createBy: 131313, //创建人
					factory: 'DC01', //盘点地点    
					inventoryLocation: 'A001,A002', //盘点库位
					countType: 3, //盘点物品类型 1.商品盘点 2.包材盘点 3.抽盘
					inventoryDirection: 1 //盘点类型 1.门店  2.供应链
				}
			}).then((res) => {
				var data = res.data;
				if (data.status == 200) {
					var obj = {};
					var result = data.result.result;
				} else {
					that.tips(that, 'warning', data.msg);
				}

			});
		},
		//选择文件
		fileChange() {
			var fileObj = document.getElementById("fileup").files[0];
			var form = new FormData();
			form.append("file", fileObj);
			this.Model.upFileForm = form;
			var that = this;
			var obj = document.getElementById("fileup");
			var len = obj.files.length;
			for (var i = 0; i < len; i++) {
				var temp = obj.files[i].name;
				that.Model.fileupName = temp;
			}
		},
		//点击上传
		uploadExcel() {
			var apiTest = this.apiTest;
			var tableData = objDeepCopy(this.Model.tableData);
			var that = this;
			this.$ajax({
				method: 'post',
				// url: '/api/whInventoryDetails/importExcel/inventory',
				url:apiTest+'/supplier/whInventoryDetails/importExcel/inventory',
				data: that.Model.upFileForm
			}).then((res) => {
				var obj = {};
				if (res.data.status == 200) {
					var result = res.data.result;
					that.tips(that, 'success', '导入成功')
					setTimeout(function() {
						that.Model.importFileDialog = false;
					}, 1000);
				} else {
					that.tips(that, 'warning', res.data.msg)
				}
			});
		},
		//下载模板(假的)
		downloadExcel() {
			var apiTest = this.apiTest;
			this.$ajax({
				method: 'get',
				url: apiTest + '/supplier/whWarehouseDetail/getGoodsExcelInfo',
			}).then((res) => {
				if (res.data.status == 200) {
					var result = res.data.result;
					if (result.fileUrl) {
						window.open(result.fileUrl);
					}

				} else {

				}
			});
		},
		//导出盘点结果
		trackingNo(type) {
			let TableSelectData = this.Model.TableSelectData;
			let arr = [];
			for (let i = 0; i < TableSelectData.length; i++) {
				arr.push(TableSelectData[i].id);
			}
			if (TableSelectData.length == 0) {
				this.tips(this, 'warning', '请勾选相应数据');
				return;
			}
			this.exportAjax(this, arr, type);
		},
		//导出盘点结果 请求
		exportAjax(that, ids, type) {
			var apiTest = this.apiTest;

			const param = {
				exportType: type,
				ids: ids
			};

			this.$ajax({
				method: 'post',
				// url: '/api/whInventoryDetails/exportExcel',
				url:apiTest+'/supplier/whInventoryDetails/exportExcel',
				data: param,
				responseType: 'arraybuffer'
			}).then((res) => {
				const data = res.data;
				let blob = new Blob([res.data], {
					type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
				});
				let objectUrl = URL.createObjectURL(blob);
				window.open(objectUrl);
			});
		},
		//点击 按钮 修改盘点单状态
		changeRowStatus(row, status) {
			var apiTest = this.apiTest;
			var that = this;
			this.$ajax({
				method: 'put',
				// url: '/api/whInventoryMain',
				url:apiTest+'/supplier/whInventoryMain',
				data: {
					id: row.id,
					inventoryStatus: status
				}
			}).then((res) => {
				let data = res.data;
				if (data.status == 200) {
					that.tips(that, 'success', '操作成功！');
					setTimeout(function() {
						that.init();
					}, 1000)
				} else {
					that.tips(that, 'warning', data.msg);
				}

			});
		},
		// 点击提交盘点报告
		submitDip(row) {
			var apiTest = this.apiTest;
			var that = this;
			this.$ajax({
				method: 'Post',
				// url: '/api/whInventoryDetails/submitInventoryReport/' + row.id + '/1',
				url: apiTest+'/supplier/whInventoryDetails/submitInventoryReport/' + row.id + '/1',
			}).then((res) => {
				let data = res.data;
				if (data.status == 200) {
					if (data.result == 1) {
						that.tips(that, 'success', '操作成功！');
						setTimeout(function() {
							that.init();
						}, 1000)
					}
				} else {
					that.tips(that, 'warning', data.msg);
				}

			});
		},
		//
		selectedHighlight(data) {
			// 如果选中则添加高亮样式
			if (this.Model.TableSelectData.indexOf(data.row) > -1) {
				return {
					"background-color": "#FEF6EE"
				};
				return {}
			}
		},
		warehousing(row) {
			this.Model.warehousingDialogStatus = row.status;
			this.Model.warehousingDialogFlag = true;
		},
		handleSelectionChange(val) {
			this.Model.TableSelectData = val;
		},
		handleCurrentChange(val) {

		},
		handleSizeChange(val) {

		},
		cardClick(color) {
			this.Model.cardSelected = color;
		},
		showChild(val) {

		},
		toSearch(val) {

		},
		toClear(val) {

		},
		toExport(val) {
			console.log(val);
		},
		tips(that, type, message) {
			let duration;
			type == 'success' ? duration = 3000 : duration = 10000;
			that.$message({
				message: message,
				type: type,
				duration: duration
			});
		},
	},
	watch: {
		$route: {
			handler(newValue, oldValue) {
				this.init();
			},
			deep: true
		},

	},
	filters: {
		objFilters: function(value) {

		}
	}
}
export default Controller;
